//3.3 /lib/code2/main_data35.dart
//bottomNavigationBar配制底部导航栏菜单
import 'dart:async';
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_global.dart';
import 'package:flutter_learn_vi/src/common/global.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/page/catalogue/catalogue_main_page.dart';
import 'package:flutter_learn_vi/src/page/common/app_upgrade.dart';
import 'package:flutter_learn_vi/src/page/common/common_dialog.dart';
import 'package:flutter_learn_vi/src/page/login/bubble_login_page.dart';
import 'package:flutter_learn_vi/src/page/mine/question_model.dart';
import 'package:flutter_learn_vi/src/page/news/news_detaile_page.dart';
import 'package:flutter_learn_vi/src/page/pending/PendingItemPage.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import '../mine/mine_main_page.dart';
import 'home_item_page.dart';

//lib/src/page/home/home_main_page.dart
//主页面的根布局
class HomeMainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeMainState();
  }
}

class _HomeMainState extends State<HomeMainPage> with MineQuestionModel {
  //当前显示页面的标签
  int _tabIndex = 0;

  //[PageView]使用的控制器
  PageController _pageController = PageController();

  //底部导航栏使用到的图标
  List<Icon> _normalIcon;
  List<BottomNavigationBarItem> _bottomlist;
  List<Widget> _normalHomeBody;

  //底部导航栏使用到的标题文字
  List<String> _normalTitle;

  StreamSubscription _homeSubscription;

  @override
  void initState() {
    super.initState();
    _homeSubscription =
        rootStreamController.stream.listen(streamListenFunction);

    initHomeData();
    getQuestion();
    getStartPic();

    Future.delayed(Duration.zero, () {
      checkAppVersion(context);
    });
  }

  void streamListenFunction(GlobalBean globalBean) {
    //刷新消息
    LogUtils.e("消息传递 刷新消息 ${globalBean.data.toString()}");
    if (globalBean.code == 202) {
      //清除缓存用户信息
      UserHelper.getInstance.exitLogin();
      //跳转登录页面
      NavigatorUtils.openPageByFade(
        context,
        BobbleLoginPage(),
        mills: 1000,
        endMills: 800,
        isReplace: true,
      );
    } else if (globalBean.code == 203) {
      getQuestion();
      getStartPic();
    }
  }

  initHomeData() {
    int isCheck = UserHelper.getInstance.userBean.isCheck;

    if (isCheck == 1) {
      checkHomeData();
    } else {
      checkPassData();
    }
  }

  void checkHomeData() {
    _normalTitle = [
      "学习",
      "动态",
      "我的",
    ];
    _normalIcon = [
      Icon(Icons.home),
      Icon(Icons.pending_sharp),
      Icon(Icons.people)
    ];

    _normalHomeBody = <Widget>[
      //第一个页面
      HomeItemMainPage(),
      PendingItemPage(),
      //个人中心
      MineMainPage(),
    ];
    _bottomlist = <BottomNavigationBarItem>[
      new BottomNavigationBarItem(label: _normalTitle[0], icon: _normalIcon[0]),
      new BottomNavigationBarItem(label: _normalTitle[1], icon: _normalIcon[1]),
      new BottomNavigationBarItem(icon: _normalIcon[2], label: _normalTitle[2]),
    ];
  }

  void checkPassData() {
    _normalTitle = [
      "首页",
      "动态",
      "党建",
      "新闻",
      "我的",
    ];
    _normalIcon = [
      Icon(Icons.home),
      Icon(Icons.pending_sharp),
      Icon(Icons.markunread_mailbox_outlined),
      Icon(Icons.list),
      Icon(Icons.people)
    ];

    _normalHomeBody = <Widget>[
      //第一个页面
      HomeItemMainPage(),
      //第二个页面
      PendingItemPage(),
      //第二个页面
      CatalogueMainPage(1),
      CatalogueMainPage(2),
      //个人中心
      MineMainPage(),
    ];

    _bottomlist = <BottomNavigationBarItem>[
      new BottomNavigationBarItem(label: _normalTitle[0], icon: _normalIcon[0]),
      new BottomNavigationBarItem(icon: _normalIcon[1], label: _normalTitle[1]),
      new BottomNavigationBarItem(icon: _normalIcon[2], label: _normalTitle[2]),
      new BottomNavigationBarItem(icon: _normalIcon[3], label: _normalTitle[3]),
      new BottomNavigationBarItem(icon: _normalIcon[4], label: _normalTitle[4]),
    ];
  }

  @override
  void dispose() {
    super.dispose();
    //移除监听
    _homeSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    //Scaffold 用来搭建页面的主体结构
    return Scaffold(
      //页面的主内容区
      //可以是单独的StatefulWidget 也可以是当前页面构建的如Text文本组件
      body: PageView(
        //设置PageView不可滑动切换
        physics: NeverScrollableScrollPhysics(),
        //PageView的控制器
        controller: _pageController,
        //PageView中的三个子页面
        children: _normalHomeBody,
      ),
      //底部导航栏
      bottomNavigationBar: buildBottomNavigation(),
    );
  }

  //lib/src/page/home/home_main_page.dart
  //构建底部导航栏
  BottomNavigationBar buildBottomNavigation() {
    //创建一个 BottomNavigationBar
    return new BottomNavigationBar(
      selectedItemColor: Colors.green,
      items: _bottomlist,
      //显示效果
      type: BottomNavigationBarType.fixed,
      //当前选中的页面
      currentIndex: _tabIndex,
      //图标的大小
      iconSize: 24.0,
      //点击事件
      onTap: (index) {
        //切换PageView中的页面显示
        _pageController.jumpToPage(index);
        _tabIndex = index;
        setState(() {});
      },
    );
  }



  void messageController(Map<String, dynamic> message) {
    String title = message["alert"];
    var extras = message["extras"];
    var pushModel = extras["cn.jpush.android.EXTRA"];
    final map = jsonDecode(pushModel);
    String pushModelString = map["pushModel"];
    final map2 = jsonDecode(pushModelString);
    final url = map2["url"];
    final newsId = map2["newsId"];
    showCommonAlertDialog(
        context: context,
        contentMessag: title,
        selectText: "去查看",
        cancleText: "不感兴趣",
        selectCallBack: () {
          if (newsId == null) {
            ToastUtils.showToast("暂无详情查看");
            return;
          }

          NavigatorUtils.pushPage(
            context,
            NewsDetailePage(
              newsId: newsId,
              screenSize: MediaQuery.of(context).size,
            ),
          );
        });
  }
}
